package generadorDescriptores.database;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;

public class ResultSetWrapper {
	
	ResultSet resultset; 
	
	public ResultSetWrapper(ResultSet result){
		resultset = result; 
	}
	
	public boolean hasNext() {
		Boolean hasNext = null;
		try {
			hasNext = this.resultset.next();
			this.resultset.previous();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return hasNext;
	}

	public void next() {
		try {
			this.resultset.next();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public <T> T getValue(String key, Class<T> returnType){
		Object keyObject = null;
		try {
			keyObject =  this.resultset.getObject(key);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return returnType.cast(keyObject);
	}
	
	public ResultSet getResultSet(){
		return this.resultset;
	}
	
	public Object getNext(String columnName){
		Object value = null; 
		ResultSetWrapper res = new ResultSetWrapper(this.resultset);
		if (res.hasNext()){
			res.next();
			try {
				value = res.getResultSet().getObject(columnName);
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null, "Error al procesar el ResultSet:\n"+e.getMessage());
			}
		}
		return value;		
	}
	
	
	public Object getNext(int column){
		Object value = null; 
		ResultSetWrapper res = new ResultSetWrapper(this.resultset);
		if (res.hasNext()){
			res.next();
			try {
				value = res.getResultSet().getObject(column);
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(null, "Error al procesar el ResultSet:\n"+e.getMessage());
			}
		}
		return value;		
	}
	
	
}
